/*$(function () {
//    发送短信验证码
    $("#codeBtn").click(function () {
        var phone = $("#phone").val();
        $.post(basePath + "/user/getCode", {"phone": phone}, function (result) {
            if (result.code == 100) {
                alert("确保手机号正确");
            }
            console.log(result);
        });
    });
    $("#registerBtn").click(function () {
        var phone = $("#phone").val();
        var password = $("#password").val();
        var code = $("#code").val();
        $.post(basePath + "/user/register",
            {
                "phone": phone,
                "password": password,
                "code": code
            }, function (result) {
                if (result == 1) {
                    alert("注册成功");
                }
            });
    });
});*/
var InterValObj; //timer变量，控制时间
var count = 60; //间隔函数，1秒执行
var curCount;//当前剩余秒数
//var code = ""; //验证码
//var codeLength = 5;//验证码长度
$(function () {
    //    发送短信验证码
    $("#codeBtn").click(function () {
        curCount = count;
        var phone = $("#phone").val();
        if (checkPhone(phone)) {
            $.post(basePath + "/user/getCode", {"phone": phone}, function (result) {
                if (result.code == 100) {
                    alert("确保手机号正确");
                }
                console.log(result);
            });
            $(this).attr("disabled", "true");
            $(this).val( curCount + "s");
            InterValObj = window.setInterval(SetTime, 1000);//启动计时器，1秒执行一次
            //向后台发送处理数据
        }
    });


    //注册
    $("#registerBtn").click(function () {
        var phone = $("#phone").val();
        var password = $("#password").val();
        var passwordRepeat = $("#passwordRepeat").val();
        var code = $("#code").val();
        if (passwordconfirm(password, passwordRepeat)) {
            if ($("#phone-reader-me").prop("checked")) {
                $.post(basePath + "/user/register",
                    {
                        "phone": phone,
                        "password": password,
                        "code": code
                    }, function (result) {
                        if (result == 1) {
                            alert("注册成功");
                            window.location.href = basePath + "/user/login";
                        } else {
                            alert("用户已存在！");
                        }
                    });
                // console.log($("#phone-reader-me").prop("checked"));
                // console.log($("input:checkbox[id='#phone-reader-me']:checked"));
            } else {
                alert("请同意《服务协议》")
            }
        } /*else {
            alert("密码不一致!");
        }*/
    });
    /* } else {
         alert("手机号码错误！");
     }*/
});



function passwordconfirm(password, passwordRepeat) {
    if (password == "" || passwordRepeat == "") {
        $("#password").empty()
        alert("密码不能为空！");
        return false;
    } else if (password != passwordRepeat) {
        alert("密码不一致!");
        return false;
    }
    return true;
}

function SetTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);//停止计时器
        $("#codeBtn").removeAttr("disabled");
        $("#codeBtn").val("重发");
    } else {
        curCount--;
        $("#codeBtn").val(curCount + "s");
    }
}

//正则校验
function checkPhone(phone) {
    if (!(/^1[3|4|5|7|8]\d{9}$/.test(phone))) {
        alert("请仔细核对自己的号码哦");
        return false;
    }
    return true;
}